package view;

import assets.Icons;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainPage extends JFrame implements WindowListener, ActionListener, BasePage {
    public static String token = "";
    public static String username = "";

    private ClipBoardView clipBoardView;
    private SystemTray tray;
    private TrayIcon trayIcon;
    private MenuItem group;
    private MenuItem register;
    private MenuItem login;
    private MenuItem exit;
    public MainPage(){
        super();
        initStyle();
        init();
        JOptionPane.showMessageDialog(null, "软件开始运行,请在任务栏查看详情");
    }
    public void initStyle(){
        //插入剪切板图标
        try {
            tray = SystemTray.getSystemTray();
            trayIcon = new TrayIcon(Icons.clipboard.getImage(), "云贴");
            trayIcon.setImageAutoSize(true);
            tray.add(trayIcon);
        } catch (AWTException e){
            e.printStackTrace();
            System.out.println("系统不支持trayIcon");
        }
        //ui采用系统风格
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public void init(){
        PopupMenu popupMenu = new PopupMenu();
        group = new MenuItem("加入群组");
        group.addActionListener(this);
        group.setActionCommand("group");
        login = new MenuItem("登录");
        login.setActionCommand("login");
        login.addActionListener(this);
        register = new MenuItem("注册");
        register.addActionListener(this);
        register.setActionCommand("register");
        exit = new MenuItem("退出");
        exit.setActionCommand("exit");
        exit.addActionListener(this);
        popupMenu.add(login);
        popupMenu.add(register);
        popupMenu.add(group);
        popupMenu.add(exit);
        trayIcon.setPopupMenu(popupMenu);
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1){
                    if (clipBoardView == null){
                        clipBoardView = new ClipBoardView();
                    }
                    clipBoardView.show();
                }
            }
        });
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        tray.remove(trayIcon);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()){
            case "exit":
                tray.remove(trayIcon);
                System.exit(0);
                break;
            case "login":
                LoginDialog.showLoginDialog(this,null);
                break;
            case "register":
                RegisterDialog.showRegisterDialog(this,null);
                break;
            case "group":
                break;
        }
    }

    @Override
    public void updatePage() {
        if (username.length() > 0){
            login.setLabel("退出登录");
        }
    }
}
